<?php

namespace app\service;

use app\dao\CareerDao;
use app\dao\CertificateDao;
use app\model\CareerShowcase;
use think\facade\Cache;

use think\cache\driver\Redis;
use think\Exception;

class CareerService
{
    private $cdao;
    public function __construct(CareerDao $careerDao)
    {
        $this->cdao= $careerDao;
    }


    // 根据id获取职业信息
    public function getCareer($cid) {
        $data = $this->cdao->getDataById($cid); // 获取到数据

        try {
            // 存数据到redis
            $cacheKey = 'career:' . $cid;
            Cache::store('redis')->set($cacheKey, $data, 3600);

        } catch (\Exception $e) {
            // 回滚
            trace('Redis缓存失败：' . $e->getMessage(), 'error');
        }

        return $data;
    }

    // 根据id更新职业信息
    public function updartCareer($cid,$data){
        $data = $this->cdao->upDataById($cid,$data);

        try {
            // 存数据到redis
            $cacheKey = 'career:' . $cid;
            Cache::store('redis')->set($cacheKey, $data, 3600);

        } catch (\Exception $e) {
            // 回滚
            trace('Redis缓存失败：' . $e->getMessage(), 'error');
        }

        return $data;
    }

    public function createCareer(array $data)
    {
        try {
            return $this->cdao->create([
                'member_id' => $data['member_id'],
                'company'   => $data['company'],
                'position'  => $data['position'],
                'salary'    => $data['salary'] ?? '',
                'story'     => $data['story'] ?? ''
            ]);
        } catch (\Exception $e) {
            throw new Exception("创建记录失败: " . $e->getMessage());
        }
    }
}